/*
 * 求一个数的二进制表示法中1的个数
 *
 */

#include <stdio.h>

//最佳算法，通过数的性质求解
int number_of_1_in_binary(int num);
//通过移位算法进行求解
int number_of_1_in_binary_other(int num);

int number_of_1_in_binary(int num)
{
    int count = 0;
    while (num) {
        count++;
        num &= num-1;
    }
    return count;
}

int number_of_1_in_binary_other(int num)
{
    int count =0;
    while(num) {
        count += num & 0x01;
        num >>= 1;
    }
    return count;
}

int main()
{
    int i = 0x10100011;
    printf("%d\n", number_of_1_in_binary(i));
    printf("%d\n", number_of_1_in_binary_other(i));
}
